/*
 * @Description: 可以直接拿去用的 Widget
 */

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

abstract  class MyClass1{
   String onSend(){
  }
}

class VercodeTimerWidget extends StatefulWidget {
  MyClass1 getVCode;
  VercodeTimerWidget(     this.getVCode ,{
    this.countdown: 60,
  });

  /// 倒计时的秒数，默认60秒。
  final int countdown;

  _VercodeTimerWidgetState createState() => _VercodeTimerWidgetState();
}
final TextStyle _availableStyle = TextStyle(
  fontSize: 12.0,
  color: Colors.green,
);
final TextStyle _unavailableStyle = TextStyle(
  fontSize: 12.0,
  color: const Color(0xFFCCCCCC),
);

class _VercodeTimerWidgetState extends State<VercodeTimerWidget> {

  Timer _timer;
  TextStyle inkWellStyle = _availableStyle;
  //倒计时数值
    int  _countdownTime = 0;
  @override
  Widget build(BuildContext context) {
    return SizedBox(
            height: 20,
        child:  FlatButton(
      splashColor: Colors.transparent,
      highlightColor: Colors.transparent,
      hoverColor: Colors.transparent,
      padding: EdgeInsets.only(right: 0),
      child: Text(handleCodeAutoSizeText(),
      style: inkWellStyle),
      onPressed: () {
        if (_countdownTime == 0) {
         widget.getVCode.onSend();
          startCountdown();
        }
      },
    ));
  }

  String handleCodeAutoSizeText() {
    if (_countdownTime > 0) {
      return '  $_countdownTime s后重新获取';
    }  else
      return '获取验证码';
  }

  //倒计时方法
  startCountdown() {
    //倒计时时间
    _countdownTime = 60;
    setState(() {
      inkWellStyle=_unavailableStyle;
    });
    final call = (timer) {
      if (_countdownTime < 1) {
        cancle();
      } else {
        setState(() {
          _countdownTime --;
//          inkWellStyle=_unavailableStyle;
        });
      }
    };
    if (_timer == null) {
      _timer = Timer.periodic(Duration(seconds: 1), call);
    }
  }

  void cancle(){
    if (_timer != null) {
      _timer.cancel();
      _timer=null;
      setState(() {
        inkWellStyle = _availableStyle;
      });
    }
  }


  @override
  void dispose() {
    cancle();
    super.dispose();

  }
}